package com.wl.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.wl.dao.CommentDao;
import com.wl.dao.NewsCategoryDao;
import com.wl.dao.NewsInfoDao;
import com.wl.enums.Status;
import com.wl.model.Comment;
import com.wl.model.NewsCategory;
import com.wl.model.NewsInfo;
import com.wl.service.CommentService;
import com.wl.service.NewsInfoService;
import com.wl.utils.PageUtil;
import com.wl.vo.NewsInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;

/**
 * @author lb
 * @date 2018/4/17 18:04
 */
@Service("newInfoServiceImpl")
public class NewInfoServiceImpl implements NewsInfoService{

    @Autowired
    private NewsInfoDao newsInfoDao;

    @Autowired
    private NewsCategoryDao newsCategoryDao;

    @Autowired
    private CommentService commentService;

    @Override
    public List<NewsInfo> getLimitNewsInfo(PageUtil pageUtil) {
        return newsInfoDao.getLimitNewsInfo(pageUtil);
    }

    @Override
    public int getCountNewsInfo() {
        return newsInfoDao.getCountNewsInfo();
    }

    @Override
    public List<NewsInfo> findByNewsInfo(NewsInfo newsInfo) throws UnsupportedEncodingException {
            return newsInfoDao.findByNewsInfo(newsInfo);

    }

    @Override
    public void deleteNewsInfo(long newsId) {
        newsInfoDao.deleteNewsInfo(newsId);
    }

    @Override
    public void addNewsInfo(NewsInfo newsInfo) throws UnsupportedEncodingException {

            NewsInfo codeNewsInfo = this.codeNewsInfo(newsInfo);
            long by = 1;
            codeNewsInfo.setCreateBy(by);
            codeNewsInfo.setUpdateBy(by);
            codeNewsInfo.setCreateTime(new Date());
            codeNewsInfo.setUpdateTime(new Date());
            newsInfoDao.insert(codeNewsInfo);

    }

    @Override
    public void updateNewsInfo(NewsInfo newsInfo) throws UnsupportedEncodingException {
//        NewsInfo codeNewsInfo = this.codeNewsInfo(newsInfo);
//        newsInfoDao.updateById(codeNewsInfo);
        newsInfoDao.updateById(newsInfo);
    }

    @Override
    public NewsInfo getNewsInfo(long newsId) {
        return newsInfoDao.selectById(newsId);
    }

    @Override
    public List<NewsInfo> allNewsInfo() {
        Wrapper<NewsInfo> wrapper = new EntityWrapper<NewsInfo>();
        wrapper.eq("status", Status.enabled.name());
        wrapper.orderBy("news_id",false);
        return newsInfoDao.selectList(wrapper);
    }

    @Override
    public List<NewsCategory> allNewsCategory() {
        Wrapper<NewsCategory> wra = new EntityWrapper<NewsCategory>();
        return newsCategoryDao.selectList(wra);
    }

    private NewsInfo codeNewsInfo(NewsInfo newsInfo) throws UnsupportedEncodingException{
        newsInfo.setTitle(newsInfo.getTitle());
        newsInfo.setOutLink(newsInfo.getOutLink());
        newsInfo.setContent(newsInfo.getContent());
        return newsInfo;
    }

    @Override
    public NewsInfoVo newsInfoDetail(Long newsId) {
        NewsInfo newsInfo = newsInfoDao.selectById(newsId);
        NewsInfoVo vo = new NewsInfoVo();
        setVo(newsInfo, vo);
        List<Comment> byPid = commentService.findByPid(newsInfo.getNewsId());
        vo.setCommentList(byPid);
        return vo;
    }

    private void  setVo(NewsInfo newsInfo,NewsInfoVo vo){
        vo.setNewsId(newsInfo.getNewsId());
        vo.setContent(newsInfo.getContent());
        vo.setOutLink(newsInfo.getOutLink());
        vo.setTitle(newsInfo.getTitle());
        vo.setCategoryId(newsInfo.getCategoryId());
        vo.setImage(newsInfo.getImage());
    }
}
